package com.hivekion.system.controller;

import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.SysStatisticsTarget;
import com.hivekion.system.domain.SysStatisticsTargetSum;
import com.hivekion.system.domain.vo.*;
import com.hivekion.system.service.ISysStatisticsTargetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/statisticsTarget")
public class SysStatisticsTargetController {
      @Autowired
      ISysStatisticsTargetService statisticsTargetService;

      @GetMapping("/getList")
      @AutoLog(value = "获取指标列表", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标管理")
      public List<SysStatisticsTargetViewVo> getList() {
	    return statisticsTargetService.getTargetListByClassic(null);
      }

      @ApiOperation(value = "通过指标分类获取列表", notes = "classicIds可以通过 ， 拼接多个classic")
      @GetMapping("/getListByClassic")
      @AutoLog(value = "通过指标分类获取指标列表", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标管理")
      public List<SysStatisticsTargetViewVo> getListByClassic(String classicIds) {
	    return statisticsTargetService.getTargetListByClassic(classicIds);
      }

      @PostMapping("/add")
      @PreAuthorize("@Permission.hasPermi('target:add')")
      @AutoLog(value = "新增指标", operationType = OperationTypeEnum.INSERT, module = "系统管理/指标管理")
      public boolean add(@RequestBody SysStatisticsTargetCreateInputVo inputVo) {
        /*if (statisticsTargetService.checkTargetNameUnique(inputVo.getName(), "")) {
            throw new BusinessException(500, "统计指标名称重复");
        }*/
	    return statisticsTargetService.create(inputVo);
      }

      @PutMapping("/edit")
      @PreAuthorize("@Permission.hasPermi('target:edit')")
      @AutoLog(value = "更改指标信息", operationType = OperationTypeEnum.UPDATE, module = "系统管理/指标管理")
      public boolean update(@RequestBody SysStatisticsTargetUpdateInputVo inputVo) {
	    return statisticsTargetService.update(inputVo);
      }

      @GetMapping("/getInfo")
      @AutoLog(value = "获取单个指标信息", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标管理")
      public SysStatisticsTarget getInfo(String targetId) {
	    return statisticsTargetService.getInfo(targetId);
      }

      ;

      @PostMapping("/checkNameUnique")
      public boolean checkNameUnique(String itemName, String itemId) {
	    return statisticsTargetService.checkTargetNameUnique(itemName, itemId);
      }

      @DeleteMapping("/remove")
      @PreAuthorize("@Permission.hasPermi('target:edit')")
      @AutoLog(value = "删除指标", operationType = OperationTypeEnum.DELETE, module = "系统管理/指标管理")
      public boolean delete(String targetId) {
	    // TODO 需要删除 统计项和子项的所有数据
	    return statisticsTargetService.delete(targetId);
      }

      @GetMapping("/getSumList")
      @AutoLog(value = "获取计算指标列表", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标管理")
      public List<SysStatisticsTargetSumViewVo> getSumList(String biNameId) {
	    return statisticsTargetService.getTargetSumList(biNameId);
      }

      @PostMapping("/addSum")
      @PreAuthorize("@Permission.hasPermi('target:add')")
      @AutoLog(value = "新增计算指标", operationType = OperationTypeEnum.INSERT, module = "系统管理/指标管理")
      public boolean add(@RequestBody SysStatisticsTargetSumCreateInputVo inputVo) {
	    if (statisticsTargetService.checkTargetSumNameUnique(inputVo.getName(), "")) {
		  throw new BusinessException(500, "统计计算指标名称重复");
	    }
	    return statisticsTargetService.createSum(inputVo);
      }

      @GetMapping("/getSumInfo")
      @AutoLog(value = "获取单个计算指标信息", operationType = OperationTypeEnum.SELECT, module = "系统管理/统计项管理")
      public SysStatisticsTargetSum getSumInfo(String targetId) {
	    return statisticsTargetService.getSumInfo(targetId);
      }

      ;

      @DeleteMapping("/removeSum")
      @PreAuthorize("@Permission.hasPermi('target:edit')")
      @AutoLog(value = "删除计算指标", operationType = OperationTypeEnum.DELETE, module = "系统管理/统计项管理")
      public boolean deleteSum(String targetId) {
	    // TODO 需要删除 统计项和子项的所有数据
	    return statisticsTargetService.deleteSum(targetId);
      }

      @PostMapping("/checkSumNameUnique")
      public boolean checkSumNameUnique(String itemName, String itemId) {
	    return statisticsTargetService.checkTargetSumNameUnique(itemName, itemId);
      }
}
